using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class ThamSoDAO
    {
        private ThamSoDTO[] ConvertDTtoArr(DataTable dt)
        {
            int n = dt.Rows.Count;
            ThamSoDTO[] KH = new ThamSoDTO[n];
            for (int i = 0; i < n; i++)
            {
                KH[i] = new ThamSoDTO();
                KH[i].MaTS = Convert.ToInt32(dt.Rows[i]["MaTS"].ToString());
                KH[i].TenTS = dt.Rows[i]["TenTS"].ToString();
                KH[i].GiaTriTS = dt.Rows[i]["GiaTriTS"].ToString();               
            }
            return KH;
        }
        public ThamSoDTO[] LayDanhSachTS()
        {
            ThamSoDTO[] KH = new ThamSoDTO[100];
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();
                DataTable dt = new DataTable();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_layDSThamSo";
                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                KH = ConvertDTtoArr(dt);
                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return KH;
        }
        public void ThemThamSo(string TenTS, string GiaTri)
        {
            try
            {
                ThamSoDTO tsDTO = new ThamSoDTO();
                tsDTO.TenTS = TenTS;
                tsDTO.GiaTriTS = GiaTri;
                ThemThamSo(tsDTO);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void ThemThamSo(ThamSoDTO kh)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_ThemThamSo";

                cmd.Parameters.Add("@TenTS", SqlDbType.NVarChar);
                cmd.Parameters.Add("@GiaTriTS", SqlDbType.NVarChar);

                cmd.Parameters["@TenTS"].Value = kh.TenTS;
                cmd.Parameters["@GiaTriTS"].Value = kh.GiaTriTS;
               

                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        public void CapNhatThamSo(ThamSoDTO kh)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_CapNhatThamSo";

                cmd.Parameters.Add("@MaTS", SqlDbType.Int);
                cmd.Parameters.Add("@TenTS", SqlDbType.NVarChar);
                cmd.Parameters.Add("@GiaTriTS", SqlDbType.NVarChar);

                cmd.Parameters["@MaTS"].Value = kh.MaTS;
                cmd.Parameters["@TenTS"].Value = kh.TenTS;
                cmd.Parameters["@GiaTriTS"].Value = kh.GiaTriTS;

                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
    }
}
